package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.DocDirectory;
import com.ruoyi.business.domain.vo.DocDirectoryVo;
import com.ruoyi.business.mapper.DocDirectoryMapper;
import com.ruoyi.business.service.IDocDirectoryService;
import com.ruoyi.common.utils.DateExtUtils;
import com.ruoyi.common.utils.TreeBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 档案目录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-07
 */
@Service
public class DocDirectoryServiceImpl implements IDocDirectoryService {
    @Autowired
    private DocDirectoryMapper docDirectoryMapper;

    /**
     * 查询档案目录
     *
     * @param id 档案目录ID
     * @return 档案目录
     */
    @Override
    public DocDirectoryVo selectDocDirectoryById(Long id) {
        return docDirectoryMapper.selectVoById(id);
    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @param query 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<Tree<Long>> selectDeptTreeList(DocDirectory query) {
        List<DocDirectoryVo> docDirectoryVos = docDirectoryMapper.selectVoList(this.buildQueryWrapper(query));
        if (CollUtil.isEmpty(docDirectoryVos)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(docDirectoryVos, (directory, tree) ->
                tree.setId(directory.getId())
                        .setParentId(directory.getParentId())
                        .setName(directory.getName())
                        .setWeight(directory.getId())
        );
    }


    /**
     * 查询档案目录列表
     *
     * @param docDirectory 档案目录
     * @return 档案目录
     */
    @Override
    public List<DocDirectoryVo> selectDocDirectoryList(DocDirectory docDirectory) {
        return docDirectoryMapper.selectVoList(this.buildQueryWrapper(docDirectory));
    }

    private QueryWrapper<DocDirectory> buildQueryWrapper(DocDirectory docDirectory) {

        QueryWrapper<DocDirectory> queryWrapper = new QueryWrapper<DocDirectory>();
        queryWrapper.lambda().like(StrUtil.isNotBlank(docDirectory.getName()), DocDirectory::getName, docDirectory.getName())
                .eq(BeanUtil.isNotEmpty(docDirectory.getParentId()), DocDirectory::getParentId, docDirectory.getParentId());
        return queryWrapper;
    }

    /**
     * 新增档案目录
     *
     * @param docDirectory 档案目录
     * @return 结果
     */
    @Override
    public int insertDocDirectory(DocDirectory docDirectory) {
        if (ObjectUtil.isNull(docDirectory.getId())) {
            docDirectory.setId(IdUtil.getSnowflakeNextId());
        }
        Long parentId = docDirectory.getParentId();
        DocDirectory parent = docDirectoryMapper.selectById(parentId);
        if (ObjectUtil.isNotNull(parent)) {
            docDirectory.setPath(parent.getPath() + "," + docDirectory.getId());
        } else {
            docDirectory.setPath(parentId + "," + docDirectory.getId());
        }
        docDirectory.setCreateTime(DateExtUtils.getNowDate());
        return docDirectoryMapper.insert(docDirectory);
    }

    /**
     * 修改档案目录
     *
     * @param docDirectory 档案目录
     * @return 结果
     */
    @Override
    public int updateDocDirectory(DocDirectory docDirectory) {
        Long parentId = docDirectory.getParentId();
        DocDirectory parent = docDirectoryMapper.selectById(parentId);
        if (ObjectUtil.isNotNull(parent)) {
            docDirectory.setPath(parent.getPath() + "." + docDirectory.getId());
        } else {
            docDirectory.setPath(parentId + "." + docDirectory.getId());
        }
        docDirectory.setUpdateTime(DateExtUtils.getNowDate());
        return docDirectoryMapper.updateById(docDirectory);
    }

    /**
     * 批量删除档案目录
     *
     * @param ids 需要删除的档案目录ID
     * @return 结果
     */
    @Override
    public int deleteDocDirectoryByIds(Long[] ids) {
        return docDirectoryMapper.deleteDocDirectoryByIds(ids);
    }

    /**
     * 删除档案目录信息
     *
     * @param id 档案目录ID
     * @return 结果
     */
    @Override
    public int deleteDocDirectoryById(Long id) {
        return docDirectoryMapper.deleteDocDirectoryById(id);
    }
}
